﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TMScore_Subsets
{
    // This class is responsible for generating fingerprints for a document
    class FingerprintsGenerator
    {
        const int DEFAULT_LENGTH = 3;
        const int DEFAULT_OVERLAP = 1;

        public int Length { get; set; } // indicates the length of the partial text that would be hashed
        public int Overlap { get; set; }  // indicates the overlap length used in the fingerprints algorithm

        // default constructor
        public FingerprintsGenerator()
        {
            Length = DEFAULT_LENGTH;
            Overlap = DEFAULT_OVERLAP;
        }

        /// <summary>
        /// The main function of the class. Recieves an entire text and returns its fingerprints as a dictionary,
        /// with the hashed value as the key and its total appearences being the value.
        /// </summary>
        public Dictionary<int, int> GenerateFingerprints(List<string> entireText)
        {
            Dictionary<int, int> toReturn = new Dictionary<int, int>();
            string txt = "";  // indicates a part of the entire text
            int hashValue;

            for (int i = 0; i <= entireText.Count - Length; i++)
            {

                for (int j = 0; j < Length; j++)
                {
                    txt += entireText[i + j] + " ";
                }

                txt = txt.Remove(txt.Length - 1);  // deleting the extra space from the end of the string
                hashValue = txt.GetHashCode();   // computing the hash code of the string

                if (toReturn.ContainsKey(hashValue))   // adding the fingerprint
                    toReturn[hashValue] += 1;
                else
                    toReturn.Add(hashValue, 1);

                txt = "";
                i += Overlap;
            }

            // sorting the fingerprints by their appearence in the document
            toReturn = (from entry in toReturn orderby entry.Value descending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);

            return toReturn;
        }

    }
}
